﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TrOCR.Helper;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace autoclock.Properties
{
    public partial class setForm1 : Form
    {

        public delegate void TransfDelegate(String value);
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8
        }
        public setForm1()
        {
            InitializeComponent();

            comboBox1.Items.Add("None");
            comboBox1.Items.Add("Alt");
            comboBox1.Items.Add("Ctrl");
            comboBox1.Items.Add("Shift");
            comboBox1.Items.Add("WindowsKey");

            readconfig();
            label8.Text = trackBar1.Value.ToString();

            //is.comboBox1.SelectedIndex = 3;

        }


        public void readconfig()
        {
            var value = IniHelper.GetValue("配置", "快捷键");
            if (value == "发生错误")
            {
                textBox1.Text = "S";
                IniHelper.SetValue("配置", "快捷键", "4+S");
                this.comboBox1.SelectedIndex = 3;
            }
            else
            {
                try
                {
                    string[] arr = value.Split('+');
                    textBox1.Text = arr[1];
                    //Console.WriteLine((KeyModifiers)int.Parse(arr[0]));
                    this.comboBox1.SelectedItem = ((KeyModifiers)int.Parse(arr[0])).ToString();

                }
                catch
                {
                    textBox1.Text = "S";
                    IniHelper.SetValue("配置", "快捷键", "4+S");
                    this.comboBox1.SelectedIndex = 3;
                }
            }

            //禁用快捷键
            value = IniHelper.GetValue("配置", "禁用快捷键");
            if (value == "发生错误")
            {
                checkBox1.Checked = false;
                IniHelper.SetValue("配置", "禁用快捷键", "False");
            }
            else
            {
                try
                {
                    checkBox1.Checked = bool.Parse(value);
                   
                }
                catch
                {
                    checkBox1.Checked = false;
                    IniHelper.SetValue("配置", "禁用快捷键", "False");
                }
            }

            value = IniHelper.GetValue("配置", "提醒间隔");
            if (value == "发生错误")
            {
                textBox2.Text = "30";
                IniHelper.SetValue("配置", "提醒间隔", "30");
            }
            else
            {
                try
                {
                    textBox2.Text = (int.Parse(value)).ToString();
                }
                catch
                {
                    textBox2.Text = "30";
                    IniHelper.SetValue("配置", "提醒间隔", "30");
                }
            }

            value = IniHelper.GetValue("配置", "存在时间");
            if (value == "发生错误")
            {
                textBox3.Text = "10";
                IniHelper.SetValue("配置", "存在时间", "10");
            }
            else
            {
                try
                {
                    textBox3.Text = (int.Parse(value)).ToString();
                }
                catch
                {
                    textBox3.Text = "10";
                    IniHelper.SetValue("配置", "存在时间", "10");
                }
            }

            value = IniHelper.GetValue("配置", "透明度");
            if (value == "发生错误")
            {
                trackBar1.Value = 80;
                IniHelper.SetValue("配置", "透明度", "80");
            }
            else
            {
                try
                {
                    trackBar1.Value = int.Parse(value);
                }
                catch
                {
                    trackBar1.Value = 80;
                    IniHelper.SetValue("配置", "透明度", "80");
                }
            }

            value = IniHelper.GetValue("配置", "外框长宽");
            if (value == "发生错误")
            {
                textBox4.Text = "385";
                textBox5.Text = "105";
                IniHelper.SetValue("配置", "外框长宽", "385+105");
            }
            else
            {
                try
                {
                    string[] arr2 = value.Split('+');

                    textBox4.Text = (int.Parse(arr2[0])).ToString();
                    textBox5.Text = (int.Parse(arr2[1])).ToString();
                }
                catch
                {
                    textBox4.Text = "385";
                    textBox5.Text = "105";
                    IniHelper.SetValue("配置", "外框长宽", "385+105");
                }
            }

            value = IniHelper.GetValue("配置", "时钟文字大小");
            if (value == "发生错误")
            {
                textBox8.Text = "60";
                IniHelper.SetValue("配置", "时钟文字大小", "60");
            }
            else
            {
                try
                {
                    textBox8.Text = (int.Parse(value)).ToString();
                }
                catch
                {
                    textBox8.Text = "60";
                    IniHelper.SetValue("配置", "时钟文字大小", "60");
                }
            }

            value = IniHelper.GetValue("配置", "位置");
            if (value == "发生错误")
            {
                textBox6.Text = "0";
                textBox7.Text = "20";
                IniHelper.SetValue("配置", "位置", "0+20");
            }
            else
            {
                try
                {
                    string[] arr3 = value.Split('+');

                    textBox6.Text = (int.Parse(arr3[0])).ToString();
                    textBox7.Text = (int.Parse(arr3[1])).ToString();
                }
                catch
                {
                    textBox6.Text = "0";
                    textBox7.Text = "20";
                    IniHelper.SetValue("配置", "位置", "0+20");
                }
            }
            Color ucolor = Color.FromArgb(255, 255, 255);
            value = IniHelper.GetValue("配置", "文字颜色");
            if (value == "发生错误")
            {
                IniHelper.SetValue("配置", "文字颜色", ucolor.ToArgb().ToString());
                button1.BackColor = ucolor;
                button1.ForeColor = InvertMeAColour(ucolor);
            }
            else
            {
                try
                {
                    ucolor = Color.FromArgb(int.Parse(value));
                    button1.BackColor = ucolor;
                    button1.ForeColor = InvertMeAColour(ucolor);
                }
                catch
                {
                    IniHelper.SetValue("配置", "文字颜色", ucolor.ToArgb().ToString());
                    button1.BackColor = ucolor;
                    button1.ForeColor = InvertMeAColour(ucolor);
                }
            }

            ucolor = Color.FromArgb(0, 0, 0);
            value = IniHelper.GetValue("配置", "背景颜色");
            if (value == "发生错误")
            {
                IniHelper.SetValue("配置", "背景颜色", ucolor.ToArgb().ToString());
                button2.BackColor = ucolor;
                button2.ForeColor = InvertMeAColour(ucolor);
            }
            else
            {
                try
                {
                    ucolor = Color.FromArgb(int.Parse(value));
                    button2.BackColor = ucolor;
                    button2.ForeColor = InvertMeAColour(ucolor);
                }
                catch
                {
                    IniHelper.SetValue("配置", "背景颜色", ucolor.ToArgb().ToString());
                    button2.BackColor = ucolor;
                    button2.ForeColor = InvertMeAColour(ucolor);
                }
            }


            //禁用快捷键
            value = IniHelper.GetValue("配置", "静默启动");
            if (value == "发生错误")
            {
                checkBox2.Checked = false;
                IniHelper.SetValue("配置", "静默启动", "False");
            }
            else
            {
                try
                {
                    checkBox2.Checked = bool.Parse(value);

                }
                catch
                {
                    checkBox2.Checked = false;
                    IniHelper.SetValue("配置", "静默启动", "False");
                }
            }

            value = IniHelper.GetValue("配置", "自定义时区");
            if (value == "发生错误")
            {
                numericUpDown1.Value = 0;
                IniHelper.SetValue("配置", "自定义时区", "0");
            }
            else
            {
                try
                {
                    numericUpDown1.Value = (int.Parse(value));
                }
                catch
                {
                    numericUpDown1.Value = 0;
                    IniHelper.SetValue("配置", "自定义时区", "0");
                }
            }

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label8.Text = trackBar1.Value.ToString();
        }

        private void saveconfig()
        {
            var planet = (KeyModifiers)Enum.Parse(typeof(KeyModifiers), comboBox1.SelectedItem.ToString());
            int planetNumber = (int)planet;
            IniHelper.SetValue("配置", "快捷键", (planetNumber).ToString() + "+" + textBox1.Text);
            IniHelper.SetValue("配置", "禁用快捷键", checkBox1.Checked.ToString());
            IniHelper.SetValue("配置", "提醒间隔", textBox2.Text);
            IniHelper.SetValue("配置", "存在时间", textBox3.Text);
            IniHelper.SetValue("配置", "透明度", trackBar1.Value.ToString());

            IniHelper.SetValue("配置", "外框长宽", textBox4.Text + "+" + textBox5.Text);
            IniHelper.SetValue("配置", "时钟文字大小", textBox8.Text);
            IniHelper.SetValue("配置", "位置", textBox6.Text + "+" + textBox7.Text);
            IniHelper.SetValue("配置", "文字颜色", button1.BackColor.ToArgb().ToString());
            IniHelper.SetValue("配置", "背景颜色", button2.BackColor.ToArgb().ToString());
            IniHelper.SetValue("配置", "静默启动", checkBox2.Checked.ToString());
            IniHelper.SetValue("配置", "自定义时区", numericUpDown1.Value.ToString());
        }
        private void setForm1_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveconfig();
            this.DialogResult = DialogResult.OK;
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        public static Enum GetEnumByValue(Type enumType, string value)
        {
            return Enum.Parse(enumType, value) as Enum;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            //textBox1.Text = e.KeyCode.ToString();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //textBox1.Text = e.KeyChar.ToString();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            textBox1.Text = e.KeyCode.ToString();
        }

        //反色
        Color InvertMeAColour(Color ColourToInvert)
        {
            return Color.FromArgb((byte)~ColourToInvert.R, (byte)~ColourToInvert.G, (byte)~ColourToInvert.B);
        }

        Color wzcolor = Color.FromArgb(0, 0, 0);
        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog colorDia = new ColorDialog();

            if (colorDia.ShowDialog() == DialogResult.OK)
            {
                //获取所选择的颜色
                Color colorChoosed = colorDia.Color;
                //改变panel的背景色
                //colorChoosed.
                wzcolor = colorChoosed;
                button1.BackColor = colorChoosed;
                button1.ForeColor = InvertMeAColour(colorChoosed);
            }
        }
        Color bjcolor = Color.FromArgb(0, 0, 0);
        private void button2_Click(object sender, EventArgs e)
        {
            ColorDialog colorDia = new ColorDialog();

            if (colorDia.ShowDialog() == DialogResult.OK)
            {
                //获取所选择的颜色
                Color colorChoosed = colorDia.Color;
                //改变panel的背景色
                bjcolor = colorChoosed;

                button2.BackColor = colorChoosed;
                button2.ForeColor = InvertMeAColour(colorChoosed);

            }
        }

        public event TransfDelegate TransfEven;
        private void button3_Click(object sender, EventArgs e)
        {
            saveconfig();
            TransfEven("123");
        }
    }
}
